package org.example.interfaces.web;

import io.swagger.annotations.ApiModel;
import org.example.application.CheckoutService;
import org.example.interfaces.command.CheckoutCommand;
import org.example.interfaces.dto.OrderDTO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolationException;
import javax.validation.Valid;

/**
 * @author liyishan
 * @date 2023/5/10 10:01
 * @apiNote
 */
@RestController
@ResponseBody
@ApiModel("/商品管理")
public class CheckoutController {

    @Resource
    private CheckoutService checkoutService;
    @RequestMapping(value = "/checkout",method = {RequestMethod.POST})
    @ResponseBody
    public Result<OrderDTO> checkout(@RequestBody @Valid CheckoutCommand cmd) {
        //Integer i = cmd.getEmpNo();
        try {
            OrderDTO orderDTO = checkoutService.checkout(cmd);
            return Result.successResp(orderDTO);
        } catch (ConstraintViolationException cve) {
            // 捕捉一些特殊异常，比如Validation异常
            return Result.errorResp(cve.getMessage());
        } catch (Exception e) {
            // 兜底异常捕获
            return Result.errorResp(e.getMessage());
        }

    }


}
